package com.fox.tlmallai.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;

@Configuration
public class TlmallAIConfig {
    private static final Logger logger = LoggerFactory.getLogger(TlmallAIConfig.class);

    /**
     * 初始化向量数据库，将文档中的内容转换为向量并存储到数据库中（实现此接口的Bean会在Spring Boot应用启动后自动执行run方法）
     * @param embeddingModel 嵌入模型
     * @param vectorStore 向量数据库
     * @param termsOfServiceDocs 文档资源
     * @return 命令行运行器
     */
    @Bean
    CommandLineRunner ingestTermOfServiceToVectorStore(@Qualifier("dashscopeEmbeddingModel") EmbeddingModel embeddingModel, VectorStore vectorStore,
                                                       @Value("classpath:rag/terms-of-service.txt") Resource termsOfServiceDocs) {
        return args -> {
            vectorStore.write(new TokenTextSplitter().transform(new TextReader(termsOfServiceDocs).read()));
            vectorStore.similaritySearch("取消订单").forEach(doc -> {
                logger.info("类似文档: {}", doc.getText());
            });
        };
    }

    @Bean
    public VectorStore vectorStore(@Qualifier("dashscopeEmbeddingModel") EmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }

     @Bean
     public ChatMemory chatMemory() {
         return new InMemoryChatMemory();
     }

    @Bean
    @ConditionalOnMissingBean
    public RestClient.Builder restClientBuilder() {
        return RestClient.builder();
    }

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        // 配置JSON转换器
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        return restTemplate;
    }
}
